package com.haipumi.utils;

import java.util.HashMap;
import java.util.Map;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import javax.mail.internet.MimeMessage;

import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.ui.velocity.VelocityEngineUtils;

import com.google.gson.Gson;
import com.haipumi.beans.haipumi_user;

public class ConsumerMessageListener_UserQueue implements MessageListener {   
    
	private Gson gson=new Gson();
	
	
	@Autowired
	private JavaMailSender mailSender;
	@Autowired
	private VelocityEngine velocityEngine;
	
    public void onMessage(Message message) {   
        //这里我们知道生产者发送的就是一个纯文本消息，所以这里可以直接进行强制转换，或者直接把onMessage方法的参数改成Message的子类TextMessage  
        TextMessage textMsg = (TextMessage) message;   
        System.out.println("接收到一个纯文本消息。");   
        
        MimeMessagePreparator preparator=null;
        try {   
            System.out.println("消息内容是：" + textMsg.getText());
            
            String [] strs=textMsg.getText().split("%00");
            String userinfo=strs[0];
            final String localhref=strs[1];
            
            
            final haipumi_user user=gson.fromJson(userinfo, haipumi_user.class);
            
            System.out.println(userinfo+"\t"+localhref+"\t"+user);
            
            preparator = new MimeMessagePreparator() {
  	            public void prepare(MimeMessage mimeMessage) throws Exception {
  	                MimeMessageHelper message = new MimeMessageHelper(mimeMessage,"utf-8");
  	                message.setSubject("嗨扑米欢迎您");
  	                message.setTo(user.getUlogon());
  	                message.setFrom("13272346734@163.com"); // could be parameterized...
  	                Map<String,Object> model = new HashMap<String,Object>();
  	                model.put("user", user);
  	                
  	                
//  	                String text = VelocityEngineUtils.mergeTemplateIntoString(
//  	 	                        velocityEngine, "com/haipumi/web/view/vm/user_register.vm","utf-8",model);
  	                System.out.println(localhref);
  	                String text = "<center style=\"border:blue 1px solid;background:#999; color:#fff;\">尊敬的"+user.getUlogon()+"<br/>您的嗨扑米账号已经注册成功。<br/>嗨扑米，帮助他人，手留余香，请谨记您的账号信息：<br/>登录名："+user.getUlogon()+"<br/>昵称:"+user.getUalais()+
  	        			"<br/>密码：<font color=\"#999\">"+user.getUpwd()+"</font><br/><font color=\"#fff\">（请单击以选中密码查看，请谨记您的密码）</font><br/>以上为用户基本信息，请点击下面的链接激活嗨扑米账号，以正常使用。<br/>"+
  	        			"<font>"+localhref+"cust.action?op=activateuser&ulogon="+user.getUlogon()+"</font>"+
  	        			//127.0.0.1%3A8080/haipumi_procust.action?op=activateuser&ulogon=756762990@qq.com
  	        			"<br/>用户信息不满意？请到用户个人管理页面进行更改<br/><font color=\"red\">邮件为系统发送，仅供激活用户账号，请勿回复，谢谢合作。</font> </center>";
//  	              		"<a href=\""+localhref+"cust.action?op=activateuser&ulogon="+user.getUlogon()+
  	        	
  	                
  	                message.setText(text, true);
  	                System.out.println(text);
  	            }
  	        };
  	        this.mailSender.send(preparator);
  	        
  	        
  	        
        } catch (JMSException e) {  
        	this.mailSender.send(preparator);
            throw new RuntimeException(e.getMessage());   
        }   
    }   
    
}  